package com.yanqu.road.server.cmd.activity.dunhuangshiku;

import com.yanqu.road.logic.pb.DunHuangPb;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.dunhuangshiku.DunHuangShiKuProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.dunhuangshiku.DunHuangShiKuModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.manger.activity.dunhuangshiku.DunHuangShiKuMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.date.DateHelper;

import java.util.Map;

/**
 * @Description
 * @Author cwq
 * @Data 2021/4/19 18:05
 */
@Cmd(code = GameProtocol.S_DUN_HUANG_SHI_KU_GET_USER_DATA, desc = "敦煌石窟获取玩家数据")
public class DunHuangShikuGetUserDataCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        DunHuangShiKuProto.DunHuangShiKuGetUserDataReqMsg req = DunHuangShiKuProto.DunHuangShiKuGetUserDataReqMsg.parseFrom(packet.getBytes());
        int activityId = req.getActivityId();
        ret = DunHuangShiKuMgr.canEnterActivityEndShowTime(player,activityId);
        if(ret != 0){
            return;
        }
        DunHuangShiKuProto.DunHuangShiKuGetUserDataCrossReqMsg.Builder builder = DunHuangShiKuProto.DunHuangShiKuGetUserDataCrossReqMsg.newBuilder();
        builder.setActivityId(activityId);
        // 每次进入敦煌 都要同步玩家数据
        builder.setPlayerInfo(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.getUserBaseInfo(player.getUserId(), GameServer.getInstance().getServerId())));
        // 门客总赚钱值
        builder.setEarnSpeed(player.getModule(PatronsModule.class).getTotalPatronsAbility().longValue());
        builder.setAttack(DunHuangShiKuMgr.getUserAttack(player));
        builder.setBlood(DunHuangShiKuMgr.getUserBlood(player));
        builder.setIsEnter(req.getIsEnter());
        DunHuangShiKuModule dunHuangShiKuModule = player.getModule(DunHuangShiKuModule.class);
        if(dunHuangShiKuModule.baseData != null){
            int now = DateHelper.getCurrentSecond();
            for (Map.Entry<Integer,Integer> entry : dunHuangShiKuModule.baseData.getTouchMallMap().entrySet()){
                if(now > entry.getValue() + DunHuangShiKuMgr.DUNHUANGSHIKU_MALL_KEEP_TIME){
                    // 超时不下发
                    continue;
                }
                int mallId = entry.getKey();
                int touchTime = entry.getValue();
                int touchTimes = dunHuangShiKuModule.baseData.getTouchMallTimes(mallId);
                if(touchTimes > 0) {
                    DunHuangShiKuProto.DunHuangShiKuUserMallInfo.Builder msg = DunHuangPb.parseDunHuangShiKuUserMallInfo(mallId, touchTime, touchTimes);
                    builder.addMallInfoList(msg);
                }
            }
        }
        player.sendUnionActivityGroupPacket(CrossProtocol.C2_DUN_HUANG_SHI_KU_GET_USER_DATA, builder, activityId);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            DunHuangShiKuProto.DunHuangShiKuGetUserDataRespMsg.Builder builder = DunHuangShiKuProto.DunHuangShiKuGetUserDataRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_DUN_HUANG_SHI_KU_GET_USER_DATA, builder);
        }
    }
}
